package chess;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Queen extends Piece{

	private String name;
	private String type;
	private Color color;
	private Coordinate co;
	private boolean selected;

	public Queen(String name, String type, Color color, Coordinate co){

		this.name= name;
		this.type= type;
		this.color= color;
		this.co= co;
	}

	public Set<Coordinate> getPossibleCoordinates(){
		int coX= co.getX();
		int coY= co.getY();
		Set<Coordinate> posCoords= new HashSet<Coordinate>();
		
		//Horizontal and vertical moves
		ArrayList<Coordinate> nsOccupied= new ArrayList<Coordinate>();
		ArrayList<Coordinate> ewOccupied= new ArrayList<Coordinate>();
		for (int delta= 1; coY+delta<8; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX, coY+delta)))
				nsOccupied.add(new Coordinate(coX, coY+delta));
			if (nsOccupied.isEmpty())
				posCoords.add(new Coordinate(coX, coY+delta));
			else if (GameRunner.board.isOccupied(new Coordinate(coX, coY+delta)) &&
					nsOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX, coY+delta)).getColor()!= color)
						posCoords.add(new Coordinate(coX, coY+delta));
		}
		nsOccupied.clear();
		for (int delta= 1; coY-delta>=0; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX, coY-delta)))
				nsOccupied.add(new Coordinate(coX, coY-delta));
			if (nsOccupied.isEmpty())
				posCoords.add(new Coordinate(coX, coY-delta));
			else if (GameRunner.board.isOccupied(new Coordinate(coX, coY-delta)) &&
					nsOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX, coY-delta)).getColor()!= color)
						posCoords.add(new Coordinate(coX, coY-delta));
		}
		nsOccupied.clear();
		for (int delta= 1; coX-delta>=0; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY)))
				ewOccupied.add(new Coordinate(coX-delta, coY));
			if (ewOccupied.isEmpty())
				posCoords.add(new Coordinate(coX-delta, coY));
			else if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY)) &&
					ewOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX-delta, coY)).getColor()!= color)
						posCoords.add(new Coordinate(coX-delta, coY));
		}
		ewOccupied.clear();
		for (int delta= 1; coX+delta<8; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY)))
				ewOccupied.add(new Coordinate(coX+delta, coY));
			if (ewOccupied.isEmpty())
				posCoords.add(new Coordinate(coX+delta, coY));
			else if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY)) &&
					ewOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX+delta, coY)).getColor()!= color)
						posCoords.add(new Coordinate(coX+delta, coY));
		}
		ewOccupied.clear();
		
		//Diagonal Moves
		/**for (int delta=1; (coX+delta)<8 && (coY+delta)<8; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY+delta))){
				if (GameRunner.board.getPiece(new Coordinate(coX+delta, coY+delta)).getColor()!= color)
					{
						posCoords.add(new Coordinate(coX+delta, coY+delta));
						break;
					}
				else break;
			}
			else posCoords.add(new Coordinate(coX+delta, coY+delta));
		}
		for (int delta=1; (coX+delta)<8 && (coY-delta)>=0; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY-delta))){
				if (GameRunner.board.getPiece(new Coordinate(coX+delta, coY-delta)).getColor()!= color)
					{
						posCoords.add(new Coordinate(coX+delta, coY-delta));
						break;
					}
				else break;
			}
			else posCoords.add(new Coordinate(coX+delta, coY-delta));
		}
		for (int delta=1; (coX-delta)>= 0 && (coY+delta)<8; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY+delta))){
				if (GameRunner.board.getPiece(new Coordinate(coX-delta, coY+delta)).getColor()!= color)
					{
						posCoords.add(new Coordinate(coX-delta, coY+delta));
						break;
					}
				else break;
			}
			else posCoords.add(new Coordinate(coX-delta, coY+delta));
		}
		for (int delta=1; (coX-delta)>=0 && (coY-delta)>=0; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY-delta))){
				if (GameRunner.board.getPiece(new Coordinate(coX-delta, coY-delta)).getColor()!= color)
					{
						posCoords.add(new Coordinate(coX-delta, coY-delta));
						break;
					}
				else break;
			}
			else posCoords.add(new Coordinate(coX-delta, coY-delta));
		}**/
		ArrayList<Coordinate> neOccupied= new ArrayList<Coordinate>();
		ArrayList<Coordinate> swOccupied= new ArrayList<Coordinate>();
	 	ArrayList<Coordinate> nwOccupied= new ArrayList<Coordinate>();
		ArrayList<Coordinate> seOccupied= new ArrayList<Coordinate>();
		
		for (int delta= 1; (coX+delta)<8 && (coY+delta)<8; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY+delta)))
				neOccupied.add(new Coordinate(coX+delta, coY+delta));
			if (neOccupied.isEmpty())
				posCoords.add(new Coordinate(coX+delta, coY+delta));
			else if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY+delta)) &&
					neOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX+delta, coY+delta)).getColor()!= color)
						posCoords.add(new Coordinate(coX+delta, coY+delta));
		}
		for (int delta= 1; (coX-delta)>=0 && (coY-delta)>=0; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY-delta)))
				swOccupied.add(new Coordinate(coX-delta, coY-delta));
			if (swOccupied.isEmpty())
				posCoords.add(new Coordinate(coX-delta, coY-delta));
			else if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY-delta)) &&
					swOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX-delta, coY-delta)).getColor()!= color)
						posCoords.add(new Coordinate(coX-delta, coY-delta));
		}
		for (int delta= 1; (coX-delta)>=0 && (coY+delta)<8; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY+delta)))
				nwOccupied.add(new Coordinate(coX-delta, coY+delta));
			if (nwOccupied.isEmpty())
				posCoords.add(new Coordinate(coX-delta, coY+delta));
			else if (GameRunner.board.isOccupied(new Coordinate(coX-delta, coY+delta)) &&
					nwOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX-delta, coY+delta)).getColor()!= color)
						posCoords.add(new Coordinate(coX-delta, coY+delta));
		}
		for (int delta= 1; (coX+delta)<8 && (coY-delta)>=0; delta++){
			if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY-delta)))
				seOccupied.add(new Coordinate(coX+delta, coY-delta));
			if (seOccupied.isEmpty())
				posCoords.add(new Coordinate(coX+delta, coY-delta));
			else if (GameRunner.board.isOccupied(new Coordinate(coX+delta, coY-delta)) &&
					seOccupied.size()== 1 &&
					GameRunner.board.getPiece(new Coordinate(coX+delta, coY-delta)).getColor()!= color)
						posCoords.add(new Coordinate(coX+delta, coY-delta));
		}
		return posCoords;
	}

	public void moveTo(Coordinate c){
		this.setCoordinate(c);
	}

	public Coordinate getCoordinate(){
		return co;
	}

	public String getType(){
		return type;
	}

	public void setCoordinate(Coordinate c, Grid g){
		co= c;
		g.setPieceToCoordinate(c, this);
	}
	
	public void setCoordinate(Coordinate c){
		co= c;
		GameRunner.board.setPieceToCoordinate(c,this);
	}

	public String getName(){
		return name;
	}

	public Color getColor(){
		return color;
	}
	
	public String toString(){
		return name + ", " + type + ", " + color + co.toString();
	}
	
	boolean hasMoved() {
		// TODO Auto-generated method stub
		return false;
	}
	
	@Override
	public void setSelected(boolean b) {
		selected = b;
	}
	
	@Override
	public boolean isSelected() {
		return selected;
	}


}